package jDialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import main.Group;

public class ShowPastGroups extends JDialog {
	private static final long serialVersionUID = 1L;

	private JList<String> list;

	private String path;

	public ShowPastGroups(JFrame frame, String path) {
		super(frame, "Show Groups", ModalityType.DOCUMENT_MODAL);

		this.path = path;
		this.addTopPanel();
		this.addBottomPanel();
		this.pack();
		this.setDialogPos();
		this.setVisible(true);
	}

	private void addBottomPanel() {
		JButton bttnCancel = new JButton("Cancel");
		bttnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ShowPastGroups.this.dispose();
			}
		});
		JButton bttnShow = new JButton("Show");
		bttnShow.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				File file = new File(path + "PastGroups/" + list.getSelectedValue() + ".txt");
				Scanner fileScan = null;
				try {
					fileScan = new Scanner(file);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
				JPanel groupPanel = new JPanel(new GridLayout(0, 3));
				int a = 1;
				while (fileScan.hasNext()) {
					Group g = new Group(a);
					String line = fileScan.nextLine();
					Scanner lineScan = new Scanner(line);
					lineScan.useDelimiter(",");
					while (lineScan.hasNext()) {
						g.addToGroup(lineScan.next());
					}
					groupPanel.add(g.getGroup());
					a++;
					lineScan.close();
				}
				fileScan.close();
				ShowPastGroups.this.getContentPane().removeAll();
				ShowPastGroups.this.add(groupPanel);
				ShowPastGroups.this.pack();
				ShowPastGroups.this.setDialogPos();
			}
		});
		JPanel bottom = new JPanel();
		bottom.add(bttnCancel);
		bottom.add(bttnShow);
		this.add(bottom, BorderLayout.SOUTH);
	}

	private void addTopPanel() {
		File[] files = new File(path + "PastGroups").listFiles();
		String[] data = new String[files.length];
		for (int a = 0; a < files.length; a++) {
			String temp = files[a].getName();
			data[a] = temp.substring(0, temp.indexOf("."));
		}
		list = new JList<String>(data);
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		list.setVisibleRowCount(-1);
		JScrollPane listScroller = new JScrollPane(list);
		listScroller.setPreferredSize(new Dimension(200, 150));
		JPanel top = new JPanel();
		top.add(listScroller);
		this.add(top, BorderLayout.NORTH);
	}

	private void setDialogPos() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((d.getWidth() - this.getWidth()) / 2);
		int y = (int) ((d.getHeight() - this.getHeight()) / 2);
		this.setLocation(x, y);
	}
}